package cn.yzgy.yzgyoa.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Busi工具类
 * 提供格式化时间,组装时间区间格式，组装下拉树返回格式等
 *
 * @author Yqsen
 * @since 2019-8-22 14:00:13
 */
@Component
public class ToolsUtil {
    /**
     * 转换查询的日期范围格式
     * @param status 状态
     * @param date 日期
     * @return 日期范围格式
     */
    public Instant toDate(String status,String date){
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneOffset.of("+08:00"));
        String start="start";
        String end="end";
        Instant datetime=null;
        if(start.equals(status)){
            datetime= Instant.from(dtf.parse(date+" 00:00:00"));
        }
        else if(end.equals(status)){
            datetime= Instant.from(dtf.parse(date+" 23:59:59"));
        }
        return datetime;
    }

    /**
     * 随机生成五位字母组合
     * @return 随机生成五位字母组合
     */
    public String getDataPerm(){
        Random rand = new Random();
        char[] letters=new char[]{'a','b','c','d','e','f','g','h','i',
                'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        StringBuilder dataPerm = new StringBuilder();
        int index;
        //默认为false
        boolean[] flags = new boolean[letters.length];
        int length = 5;
        for(int i=0;i<length;i++){
            do{
                index = rand.nextInt(letters.length);
            }while(flags[index]);
            char c = letters[index];
            dataPerm.append(c);
            flags[index]=true;
        }
        return dataPerm.toString();
    }
    /**
     * StringToLocalDateTime
     * @param date 日期
     * @return LocalDateTime
     */
    public LocalDateTime toLocalDateTime(String date){

        return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }


    /**
     * 组装返回数据
     * @param type 调用接口名称
     * @return 返回数据
     */
    public JSONObject getReturnData(String type){
        JSONObject json=new JSONObject();
        HashMap<String,Object> status=new HashMap<>(16);
        status.put("code","200");
        status.put("message","获取"+type+"数据成功");
        json.put("status",status);
        json.put("msg","操作成功");
        json.put("code","2000");
        json.put("operation","获取"+type+"数据");
        json.put("success","success");
        json.put("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return json;
    }

    /**
     * 获取当天的开始时间
     */
    public static Date getDayBegin() {
             Calendar cal = new GregorianCalendar();
             cal.set(Calendar.HOUR_OF_DAY, 0);
             cal.set(Calendar.MINUTE, 0);
             cal.set(Calendar.SECOND, 0);
             cal.set(Calendar.MILLISECOND, 0);
             return cal.getTime();
             }
    /**
     * 获取当天的结束时间
     */
    public static Date getDayEnd() {
         Calendar cal = new GregorianCalendar();
         cal.set(Calendar.HOUR_OF_DAY, 23);
         cal.set(Calendar.MINUTE, 59);
         cal.set(Calendar.SECOND, 59);
         return cal.getTime();
         }
    /**
     * 获取某个日期的开始时间
     * @param d date
     * @return 获取某个日期的开始时间
     */
     private static Timestamp getDayStartTime(Date d) {
        Calendar calendar=Calendar.getInstance();
        if(null!=d){
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }


    /**
     * 获取某个日期的结束时间
     * @param d date
     * @return 获取某个日期的开始时间
     */
     private static Timestamp getDayEndTime(Date d) {
        Calendar calendar=Calendar.getInstance();
        if(null!=d){
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }


    /**
     * 获取本周的开始时间
     */
    public static Date getBeginDayOfWeek() {
         Date date = new Date();
         Calendar cal = Calendar.getInstance();
         cal.setTime(date);
         int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
         if (dayofweek == 1) {
                 dayofweek += 7;
             }
         cal.add(Calendar.DATE, 2 - dayofweek);
         return getDayStartTime(cal.getTime());
     }
    /**
     * 获取本周的结束时间
     */
       public static Date getEndDayOfWeek(){
             Calendar cal = Calendar.getInstance();
             cal.setTime(getBeginDayOfWeek());
             cal.add(Calendar.DAY_OF_WEEK, 6);
             Date weekEndSta = cal.getTime();
             return getDayEndTime(weekEndSta);
         }


    /**
     * 获取今年是哪一年
     */
    private static Integer getNowYear(){
        Date date = new Date();
        GregorianCalendar gc=(GregorianCalendar)Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }


    /**
     * 获取本月是哪一月
     */
    private static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc=(GregorianCalendar)Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }


    /**
     * 获取本月的开始时间
     */
    public static Date getBeginDayOfMonth() {
        Calendar calendar=Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth()-1, 1);
        return getDayStartTime(calendar.getTime());
    }


    /**
     * 获取本月的结束时间
     */
    public static Date getEndDayOfMonth() {
        Calendar calendar=Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth()-1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth()-1, day);
        return getDayEndTime(calendar.getTime());
    }

}
